//
//  GBHTTPSessionManager.m
//  GBArchitecture
//
//  Created by ceo on 2017/6/9.
//  Copyright © 2017年 chengb. All rights reserved.
//

#import "GBHTTPSessionManager.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "NSNotificationCenter+GBNotice.h"
@implementation GBHTTPSessionManager

+ (GBHTTPSessionManager *)shared{
    
    
    static dispatch_once_t onceToken;
    static GBHTTPSessionManager *shareInstance = nil;
    dispatch_once(&onceToken, ^{
        shareInstance = [GBHTTPSessionManager manager];
        AFJSONResponseSerializer *responseSerializer =  [AFJSONResponseSerializer serializer];
        responseSerializer.removesKeysWithNullValues = YES;
        shareInstance.responseSerializer = responseSerializer;
        
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            [[NSNotificationCenter defaultCenter] postNotificationName:GBNotification_NetWorkStateChange object:nil];
            NSLog(@"网络状态: %@", AFStringFromNetworkReachabilityStatus(status));
        }];
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    });
    return shareInstance;
}



- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))downloadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure{
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    
    //处理日志
    [self handleLogWithParameters:parameters];
    
    //处理请求参数
    parameters = [self handleWithParameters:parameters];
    
    return [super GET:URLString parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self handleLogWithResponseObject:responseObject];
        if (success) {
            success(task,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self handleLogWithTask:task error:error];
        if (failure) {
            failure(task,error);
        }
    }];

}

//处理请求参数
- (id)handleWithParameters:(id)parameters{
    return parameters;
}

//处理请求日志
- (void)handleLogWithParameters:(id)parameters{
    NSLog(@"\n\n\n-----API:%@-----parameters_start",@"http://www.baidu.com");
    NSLog(@"\n---\n%@\n---\n",parameters);
    NSLog(@"\n\n\n-----API:%@-----parameters_end",@"http://www.baidu.com");
}

//处理返回信息
- (id)handleWithResponseObject:(id)responseObject{
    return responseObject;
}

//处理返回日志
- (void)handleLogWithResponseObject:(id)responseObject{
    
    
    
    NSLog(@"\n\n\n-----API:%@-----responseObject_start",@"http://www.baidu.com");
    NSLog(@"---\n%@\n---\n",[responseObject mj_JSONObject]);
    NSLog(@"\n\n\n-----API:%@-----responseObject_end",@"http://www.baidu.com");
}


//处理请求错误
- (void)handelErrorWithTask:(NSURLSessionDataTask *)task  error:(NSError *)error{
    
}

//处理返回日志
- (void)handleLogWithTask:(NSURLSessionDataTask *)task  error:(NSError *)error{
    NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
    NSDictionary *errorData = [data mj_JSONObject];
    NSLog(@"\n\n\n-----API:%@-----error_start",@"http://www.baidu.com");
    NSLog(@"---\n%@\n---\n",errorData);
    NSLog(@"\n\n\n-----API:%@-----error_end",@"http://www.baidu.com");
}



@end
